import os
import PIL.Image as img

path_ori = '/usr/VOCdevkit/VOC2019/JPEGImages/'
path_new = '/usr/VOCdevkit/VOC2019/JPEGImages/'


# fileList = os.listdir(path_ori)
# total_num = len(fileList)
# for i in range(total_num):  # read each rows
#     im = img.open(path_ori + fileList[0])
#     #ng = im.transpose(img.FLIP_LEFT_RIGHT) #左右对换。
#     # ng = im.transpose(img.FLIP_TOP_BOTTOM)  # 上下对换。
#     # ng = im.transpose(img.ROTATE_90) #_left rotate
#     ng = im.transpose(img.ROTATE_270) # right rotate
#     ng.save(path_new + str(i) +'.jpg')
# print(i)

def rotateImg(path_ori, path_new, way = 1):
    fileList = os.listdir(path_ori)
    total_num = len(fileList)
    if way == 1:         #左右对换
        for i in range(total_num):  # read each rows
            im = img.open(path_ori + fileList[i])
            ng = im.transpose(img.FLIP_LEFT_RIGHT) #左右对换。
            ng.save(path_new + str(i) + 'CLR.jpg')
            print(i)
    elif way == 2:      # 上下对换。
        for i in range(total_num):  # read each rows
            im = img.open(path_ori + fileList[i])
            ng = im.transpose(img.FLIP_TOP_BOTTOM)  # 上下对换。
            ng.save(path_new + str(i) + 'CTB.jpg')
            print(i)
    elif way == 3:      #_left rotate
        for i in range(total_num):  # read each rows
            im = img.open(path_ori + fileList[i])
            ng = im.transpose(img.ROTATE_90) #_left rotate
            ng.save(path_new + str(i) + 'LR90.jpg')
            print(i)
    elif way == 4:      # right rotate
        for i in range(total_num):  # read each rows
            im = img.open(path_ori + fileList[i])
            ng = im.transpose(img.ROTATE_270)  # right rotate
            ng.save(path_new + str(i) + 'RR90.jpg')
            print(i)

def rotateAllImg(path_ori, path_new):
    fileList = os.listdir(path_ori)
    total_num = len(fileList)
     #左右对换
    for i in range(total_num):  # read each rows
        im = img.open(path_ori + fileList[i])
        ng = im.transpose(img.FLIP_LEFT_RIGHT)  # 左右对换。
        ng.save(path_new + fileList[i].split('.')[0] + 'CLR.jpg')
        print(path_new + fileList[i].split('.')[0] + 'CLR.jpg')
     # 上下对换。
    for i in range(total_num):  # read each rows
        im = img.open(path_ori + fileList[i])
        ng = im.transpose(img.FLIP_TOP_BOTTOM)  # 上下对换。
        ng.save(path_new + fileList[i].split('.')[0] + 'CTB.jpg')
        print(path_new + fileList[i].split('.')[0]+ 'CTB.jpg')
    #_left rotate
    for i in range(total_num):  # read each rows
        im = img.open(path_ori + fileList[i])
        ng = im.transpose(img.ROTATE_90)  # _left rotate
        ng.save(path_new + fileList[i].split('.')[0] + 'LR90.jpg')
        print(path_new + fileList[i].split('.')[0] + 'LR90.jpg')
    # right rotate
    for i in range(total_num):  # read each rows
        im = img.open(path_ori + fileList[i])
        ng = im.transpose(img.ROTATE_270)  # right rotate
        ng.save(path_new + fileList[i].split('.')[0] + 'RR90.jpg')
        print(path_new + fileList[i].split('.')[0] + 'RR90.jpg')
rotateAllImg(path_ori, path_new)
# os.system('chown -R user /usr/VOCdevkit/')
os.system('chown -R user '+ path_new)